package com.shanzhu.beadhouse.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.shanzhu.beadhouse.common.util.PageUtil;
import com.shanzhu.beadhouse.dao.mapper.ServiceLogMapper;
import com.shanzhu.beadhouse.dao.mapper.ServicePurchaseMapper;
import com.shanzhu.beadhouse.entity.base.PageResult;
import com.shanzhu.beadhouse.entity.base.Result;
import com.shanzhu.beadhouse.entity.po.ServiceLog;
import com.shanzhu.beadhouse.entity.po.ServicePurchase;
import com.shanzhu.beadhouse.entity.query.AddLogQuery;
import com.shanzhu.beadhouse.entity.query.DeleteServiceLogQuery;
import com.shanzhu.beadhouse.entity.query.PageServiceLogQuery;
import com.shanzhu.beadhouse.entity.vo.PageServiceLogVo;
import com.shanzhu.beadhouse.service.IServiceLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ServiceLogServiceImpl implements IServiceLogService  {
    @Resource
    private ServiceLogMapper serviceLogMapper;

    @Resource
    private ServicePurchaseMapper servicePurchaseMapper;

    @Resource
    private PageUtil pageUtil;

    @Override
    public Result addServiceLog(AddLogQuery query) {
        //更新服务次数
        long cnt = query.getCareQuantity();
        List<ServicePurchase> usablePurchaseList = servicePurchaseMapper.getUsablePurchaseList(query.getSid(), query.getElderId());
        int size = usablePurchaseList.size();
        int index=0;
        while (cnt>0){
            ServicePurchase item = usablePurchaseList.get(index);
            if (item.getHold()<=cnt){
                UpdateWrapper uw = new UpdateWrapper<>();
                uw.set("del_flag","Y");
                uw.eq("id",item.getId());
                servicePurchaseMapper.update(null,uw);
                cnt-=item.getHold();
                index++;
            }else {
                UpdateWrapper uw = new UpdateWrapper<>();
                uw.set("hold",item.getHold()-cnt);
                uw.eq("id",item.getId());
                servicePurchaseMapper.update(null,uw);
                break;
            }
        }
        //        UpdateWrapper uw =  new UpdateWrapper<>();
//        uw.setSql("hold=hold-"+query.getCareQuantity());
//        uw.eq("id",query.getId());
//        servicePurchaseMapper.update(null,uw);
        //生成记录
        ServiceLog log = new ServiceLog();
        log.setCompleteFlag("Y");
        log.setSid(query.getSid());
        log.setElderId(query.getElderId());
        log.setStaffId(query.getStaffId());
        log.setCareQuantity(query.getCareQuantity());
        log.setCareContent(query.getCareContent());
        log.setNurseDate(query.getNurseDate());
        log.setDelFlag("N");
        serviceLogMapper.insert(log);
        return Result.success();
    }

    @Override
    public Result pageServiceLog(PageServiceLogQuery query) {
        List<PageServiceLogVo> pageServiceLogVoList;
        if (query.getElderId()!=0){
            pageServiceLogVoList = serviceLogMapper.listServiceLogByKey(query);
        }else {
            pageServiceLogVoList=new ArrayList<>();
        }

        PageResult<PageServiceLogVo> pageResult = pageUtil.packPageResultData(pageServiceLogVoList,query.getPageNum(),query.getPageSize());
        return Result.success(pageResult);
    }

    @Override
    public Result deleteServiceLog(DeleteServiceLogQuery query) {
        UpdateWrapper uw = new UpdateWrapper<>();
        uw.set("del_flag","Y");
        uw.eq("id",query.getId());
        serviceLogMapper.update(null,uw);
        return Result.success();
    }
}
